<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31 0031
 * Time: 10:09
 */
defined("IN_IA")or exit("Access denied");
checklogin();  //验证是否登录
global $_GPC,$_W;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"distribution_set";
//分销设置
if($op=='distribution_set'){
    $filed=array(
        'is_open_distribution',
        'is_check_distribution',
        'distribution_one_name',
        'distribution_one_price',
        'distribution_two_name',
        'distribution_two_price',
        'distribution_withdraw_low_price',
        'distribution_cover',
        'distribution_agreement',
    );
    include 'function.inc.php';
    $list=getFarmSetData($filed,array('uniacid'=>$uniacid));
    include $this->template('web/distribution/distribution_set');
}
//分销设置保存
if($op=='distribution_set_save'){
    $data=$_POST;
    if(empty($data['is_open_distribution'])){
        $data['is_open_distribution']=0;
    }
    if(empty($data['is_check_distribution'])){
        $data['is_check_distribution']=0;
    }
    $data['distribution_cover']=tomedia($data['distribution_cover']);
    include 'function.inc.php';
    $res=insertSetData($data,$uniacid);
    if($res){
        message('操作成功',$this->createWebUrl('distribution'));die;
    }else{
        message('操作失败');die;
    }
}

//分销商
if($op=='distribution_user'){
    if(empty($_GPC['page'])){
        $page=1;
    }else{
        $page=$_GPC['page'];
    }
    if(empty($_GPC['status'])){
        $status=2;
    }else{
        $status=$_GPC['status'];
    }
    if($status==2) {
        $all = pdo_getall('cqkundian_farm_user', array('uniacid' => $uniacid, 'is_distributor' => 1));
        $pager = pagination(count($all), $page, 10);
        $user = pdo_getall('cqkundian_farm_user', array('uniacid' => $uniacid, 'is_distributor' => 1), '', '', 'become_time desc', array($page, 10));
        for ($i = 0; $i < count($user); $i++) {
            $checkInfo = pdo_get('cqkundian_farm_distribution_check', array('uniacid' => $uniacid, 'uid' => $user[$i]['uid']));
            $user[$i]['checkInfo'] = $checkInfo;
            $user[$i]['become_time'] = date("Y-m-d H:i:s", $user[$i]['become_time']);
            $user[$i]['apply_time'] = date("Y-m-d H:i:s", $user[$i]['checkInfo']['create_time']);

            if ($user[$i]['one_distributor'] != 0) {
                $one_sale = pdo_get('cqkundian_farm_user', array('uniacid' => $uniacid, 'uid' => $user[$i]['one_distributor']));
                $user[$i]['one_sale'] = $one_sale['nickname'];
            } else {
                $user[$i]['one_sale'] = '平台';
            }
        }
    }elseif ($status==1){  //已审核
        $checkInfo = pdo_getall('cqkundian_farm_distribution_check', array('uniacid' => $uniacid, 'status'=>1));
        $user=array();
        for ($i=0;$i<count($checkInfo);$i++){
            $checkuser=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$checkInfo[$i]['uid']));
            $checkuser['checkInfo']=$checkInfo[$i];
            $checkuser['become_time'] = date("Y-m-d H:i:s", $checkuser['become_time']);
            $checkuser['apply_time'] = date("Y-m-d H:i:s", $checkInfo[$i]['create_time']);
            if ($checkuser['one_distributor'] != 0) {
                $one_sale = pdo_get('cqkundian_farm_user', array('uniacid' => $uniacid, 'uid' => $checkuser['one_distributor']));
                $checkuser['one_sale'] = $one_sale['nickname'];
            } else {
                $checkuser['one_sale'] = '平台';
            }
            $user[]=$checkuser;
        }
    }elseif ($status==3){
        $checkInfo = pdo_getall('cqkundian_farm_distribution_check', array('uniacid' => $uniacid, 'status'=>0));
        $user=array();
        for ($i=0;$i<count($checkInfo);$i++){
            $checkuser=pdo_get('cqkundian_farm_user',array('uniacid'=>$uniacid,'uid'=>$checkInfo[$i]['uid']));
            $checkuser['checkInfo']=$checkInfo[$i];
            $checkuser['become_time'] = date("Y-m-d H:i:s", $checkuser['become_time']);
            $checkuser['apply_time'] = date("Y-m-d H:i:s", $checkInfo[$i]['create_time']);
            if ($checkuser['one_distributor'] != 0) {
                $one_sale = pdo_get('cqkundian_farm_user', array('uniacid' => $uniacid, 'uid' => $checkuser['one_distributor']));
                $checkuser['one_sale'] = $one_sale['nickname'];
            } else {
                $checkuser['one_sale'] = '平台';
            }
            $user[]=$checkuser;
        }
    }
    include $this->template("web/distribution/distribution_user");
}

//通过审核
if($op=='agreeSale'){
    $id=$_GPC['id'];
    $check=pdo_get('cqkundian_farm_distribution_check',array('id'=>$id,'uniacid'=>$uniacid));
    if($check['status']==0){
        $code_path=getcqrcode($check['uid']);
        $res=pdo_update('cqkundian_farm_distribution_check',array('status'=>1),array('uniacid'=>$uniacid,'id'=>$id));
        $res1=pdo_update('cqkundian_farm_user',array('is_distributor'=>1,'share_qrcode'=>$_W['siteroot'].$code_path),array('uid'=>$check['uid'],'uniacid'=>$uniacid));
        if($res && $res1){
            echo json_encode(array('status'=>1,'msg'=>'操作成功'));die;
        }else{
            echo json_encode(array('status'=>0,'msg'=>'操作失败'));die;
        }
    }else{
        echo json_encode(array('status'=>0,'msg'=>'操作异常'));die;
    }
}
//拒绝审核
if($op=='denySale'){
    $id=$_GPC['id'];
    $check=pdo_get('cqkundian_farm_distribution_check',array('id'=>$id,'uniacid'=>$uniacid));
    if($check['status']==0){
        $res=pdo_update('cqkundian_farm_distribution_check',array('status'=>2),array('uniacid'=>$uniacid,'id'=>$id));
        if($res){
            echo json_encode(array('status'=>1,'msg'=>'操作成功'));die;
        }else{
            echo json_encode(array('status'=>0,'msg'=>'操作失败'));die;
        }
    }else{
        echo json_encode(array('status'=>0,'msg'=>'操作异常'));die;
    }
}

//分销订单
if($op=='distribution_order'){
    $page=$_GPC['page'];
    $order_type=$_GPC['order_type'] ? $_GPC['order_type'] :1;

    //查询所有分销商的订单
    $order_con=array(
        'uniacid'=>$uniacid,
        'is_price >'=>0
    );
    $order_number=$_GPC['order_number'];
    if($order_number){
        $order_con['order_number LIKE']='%'.$order_number.'%';
    }
    $orderData=array();
    if($order_type==1) {
        $all=pdo_getall('cqkundian_farm_shop_order', $order_con);
        $pager=pagination(count($all),$page,7);
        $orderData = pdo_getall('cqkundian_farm_shop_order', $order_con, '', '', 'create_time desc', array($page, 7));
        for ($m = 0; $m < count($orderData); $m++) {
            //查询用户信息
            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$m]['uid'], 'uniacid' => $uniacid));
            //一级分销信息
            $one_sale=pdo_get('cqkundian_farm_user',array('uid'=>$user['one_distributor'],'uniacid'=>$uniacid));
            if($one_sale['one_distributor']!=0){
                $two_sale=pdo_get('cqkundian_farm_user',array('uid'=>$one_sale['one_distributor'],'uniacid'=>$uniacid));
                $orderData[$m]['two_sale']=$two_sale;
            }
            $orderData[$m]['one_sale']=$one_sale;
        }
    }elseif ($order_type==2){
        $all=pdo_getall('cqkundian_farm_group_order', $order_con);
        $pager=pagination(count($all),$page,7);
        $orderData = pdo_getall('cqkundian_farm_group_order', $order_con, '', '', 'create_time desc', array($page, 7));
        for ($m = 0; $m < count($orderData); $m++) {
            //查询用户信息
            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$m]['uid'], 'uniacid' => $uniacid));
            //一级分销信息
            $one_sale=pdo_get('cqkundian_farm_user',array('uid'=>$user['one_distributor'],'uniacid'=>$uniacid));
            if($one_sale['one_distributor']!=0){
                $two_sale=pdo_get('cqkundian_farm_user',array('uid'=>$one_sale['one_distributor'],'uniacid'=>$uniacid));
                $orderData[$m]['two_sale']=$two_sale;
            }
            $orderData[$m]['one_sale']=$one_sale;
        }
    }elseif ($order_type==3){
        $animal_con=array(
            'uniacid'=>$uniacid,
            'is_price >'=>0
        );

        if($order_number){
            $animal_con['order_number LIKE']='%'.$order_number.'%';
        }
        $all=pdo_getall('cqkundian_farm_animal_order', $animal_con);
        $pager=pagination(count($all),$page,7);
        $orderData = pdo_getall('cqkundian_farm_animal_order', $animal_con, '', '', 'create_time desc', array($page, 7));
        for ($m = 0; $m < count($orderData); $m++) {
            //查询用户信息
            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$m]['uid'], 'uniacid' => $uniacid));
            $one_sale=pdo_get('cqkundian_farm_user',array('uid'=>$user['one_distributor'],'uniacid'=>$uniacid));
            if($one_sale['one_distributor']!=0){
                $two_sale=pdo_get('cqkundian_farm_user',array('uid'=>$one_sale['one_distributor'],'uniacid'=>$uniacid));
                $orderData[$m]['two_sale']=$two_sale;
            }
            $orderData[$m]['one_sale']=$one_sale;
        }
    }elseif ($order_type==4){
        $land_con=array(
            'uniacid'=>$uniacid,
            'is_price >'=>0
        );
        if($order_number){
            $land_con['order_number LIKE']='%'.$order_number.'%';
        }
        $all=pdo_getall('cqkundian_farm_animal_order', $land_con);
        $pager=pagination(count($all),$page,7);
        $orderData = pdo_getall('cqkundian_farm_land_order', $land_con, '', '', 'create_time desc', array($page, 7));
        for ($m = 0; $m < count($orderData); $m++) {
            //查询用户信息
            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$m]['uid'], 'uniacid' => $uniacid));
            $one_sale=pdo_get('cqkundian_farm_user',array('uid'=>$user['one_distributor'],'uniacid'=>$uniacid));
            if($one_sale['one_distributor']!=0){
                $two_sale=pdo_get('cqkundian_farm_user',array('uid'=>$one_sale['one_distributor'],'uniacid'=>$uniacid));
                $orderData[$m]['two_sale']=$two_sale;
            }
            $orderData[$m]['one_sale']=$one_sale;
        }
    }
    include  $this->template("web/distribution/distribution_order");

}
//查看分销订单详细信息
if($op=='distribution_order_detail'){
    $order_id=$_GPC['id'];
    $order_type=$_GPC['order_type'];
    if($order_type==1) {
        $orderData = pdo_get('cqkundian_farm_shop_order', array('uniacid' => $uniacid, 'id' => $order_id));
        $orderDetail = pdo_getall('cqkundian_farm_shop_order_detail', array('uniacid' => $uniacid, 'order_id' => $order_id));
        for ($i = 0; $i < count($orderDetail); $i++) {
            $specVal = pdo_get('cqkundian_farm_goods_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
            $spec_id = explode(",", $specVal['sku_name']);
            $spec_val = pdo_getall('cqkundian_farm_spec_value', array('id in' => $spec_id));
            $specItem = pdo_getall('cqkundian_farm_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
            for ($j = 0; $j < count($specItem); $j++) {
                for ($m = 0; $m < count($spec_val); $m++) {
                    if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                        $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                    }
                }
            }
            $orderDetail[$i]['specItem'] = $specItem;
        }
    }elseif ($order_type==2){
        $orderData = pdo_get('cqkundian_farm_group_order', array('uniacid' => $uniacid, 'id' => $order_id));
        $orderDetail = pdo_getall('cqkundian_farm_group_order_detail', array('uniacid' => $uniacid, 'order_id' => $order_id));
        for($i=0;$i<count($orderDetail);$i++){
            $specVal=pdo_get('cqkundian_farm_group_goods_spec',array('goods_id'=>$orderDetail[$i]['goods_id'],'uniacid'=>$uniacid));
            $spec_id=explode(",", $specVal['sku_name']);
            $spec_val=pdo_getall('cqkundian_farm_group_spec_value',array('id in'=>$spec_id));
            $specItem=pdo_getall('cqkundian_farm_group_spec',array('goods_id'=>$orderDetail[$i]['goods_id'],'uniacid'=>$uniacid));
            for($j=0;$j<count($specItem);$j++){
                for($m=0;$m<count($spec_val);$m++){
                    if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                        $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                    }
                }
            }
            $orderDetail[$i]['specItem']=$specItem;
        }
    }elseif ($order_type==3){
        $orderData = pdo_get('cqkundian_farm_animal_order', array('id'=>$order_id,'uniacid'=>$uniacid));
        $orderDetail = pdo_get('cqkundian_farm_animal', array('uniacid' => $uniacid, 'id' => $orderData['aid']));
        $orderData['orderDetail'] = $orderDetail;
    }elseif ($order_type==4){
        $orderData = pdo_get('cqkundian_farm_land_order',  array('id'=>$order_id,'uniacid'=>$uniacid));
        $orderDetail = pdo_getall('cqkundian_farm_land_order_detail', array('uniacid' => $uniacid, 'order_id' => $orderData['id']));
    }
    include  $this->template("web/distribution/distribution_order_detail");
}


//提现记录
if($op=='distribution_withdraw'){
    $con=array('uniacid'=>$uniacid);
    if($_GPC['uid']){
        $con['uid']=$_GPC['uid'];
    }
    $all=pdo_getall('cqkundian_farm_withdraw',$con);
    $page=$_GPC['page'] ? $_GPC['page'] :1;
    $pager=pagination(count($all),$page,10);
    $list=pdo_getall('cqkundian_farm_withdraw',$con,'','','create_time desc',array($page,10));
    for ($i=0;$i<count($list);$i++){
        $user=pdo_get('cqkundian_farm_user',array('uid'=>$list[$i]['uid']));
        $list[$i]['avatarurl']=$user['avatarurl'];
        $list[$i]['nickname']=$user['nickname'];
    }
    include $this->template('web/distribution/distribution_withdraw');
}

//已打款
if($op=='agreeWithdraw'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_withdraw',array('status'=>1),array('uniacid'=>$uniacid,'id'=>$id));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//拒绝提现
if($op=='deny_withdraw'){
    $id=$_GPC['id'];
    include $this->template('web/distribution/deny_withdraw');
}
//拒绝提现
if($op=='deny_withdraw_save'){
    $withdraw=pdo_get('cqkundian_farm_withdraw',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    $update=array(
        'remark'=>$_GPC['remark'],
        'status'=>2
    );
    $res=pdo_update('cqkundian_farm_withdraw',$update,array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    if($res){
        pdo_update('cqkundian_farm_user',array('price +='=>$withdraw['price']),array('uniacid'=>$uniacid,'uid'=>$withdraw['uid']));
        message('操作成功',url('site/entry/distribution',array('m'=>'kundian_farm','op'=>'distribution_withdraw')));
    }else{
        message('操作失败');
    }
}

/**
 * 生成分享二维码
 * @param $uid
 * @return string
 */
function getcqrcode($uid){
    $page="kundian_farm/pages/HomePage/index/index?uid=".$uid;
    $account_api = WeAccount::create();
    $response = $account_api->getCodeLimit($page, 430, array(
        'auto_color' => false,
        'line_color' => array(
            'r' => '#ABABAB',
            'g' => '#ABABAC',
            'b' => '#ABABAD',
        ),
    ));
    $filename='kundian_farm_share_uid_'.$uid;
    $filepath='/addons/kundian_farm/resource/image/'.$filename.'.png';
    file_put_contents(IA_ROOT . '/addons/kundian_farm/resource/image/'.$filename.'.png', $response);
    return $filepath;
}